/*
 * @Author: your name
 * @Date: 2021-04-18 18:07:19
 * @LastEditTime: 2021-04-20 00:11:56
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \defining_enum\src\main.rs
 */

enum Message{
    Quit,  // 没有关联任何数据
    Move {x:i32, y:i32},  // 匿名结构体
    Write(String),   // 包含单独一个String
    ChangeColor(i32, i32, i32),   // 元组结构体
}

// 方法
impl Message{
    fn prin(&self){
        // match 类似switch
        match *self{  // 解引用
            Message::Quit => println!("Quit"),
            Message::Move{x,y} => println!("Move x = {}, y = {}", x,y),
            Message::ChangeColor(a, b, c) => println!("a = {},b = {}, c = {}", a,b,c),
            _ => println!("defalut"),
        }
    }
    fn call(&self){
        // 定义方法体
    }
}
/* Option 是标准库定义的一个枚举, 形式：
enum Option<T>{
    Some(T),
    None,
}
*/

fn plus_one(x:Option<i32>) -> Option<i32>{
    match x{
        None => None,
        Some(x) => Some(x+1),
    }
}
fn main() {
    let m = Message::Write(String::from("hello"));

    m.call();
    let mymove = Message::Move{x: 2,y: 3};
    m.prin();
    mymove.prin();

    let some_number = Some(5);
    let some_string = Some(String::from("a string"));
    let absent_number:Option<i32> = None;

    let x:i32 = 5;
    let y:Option<i32> = Some(7);
    let mut temp = 0;
    match y{
        Some(i) => {temp = i;},
        None => {println!{"do nothing"};},
    }
    let mut sum = x+temp;
    println!("sum = {}",sum);
    let result = plus_one(y);
    match result{
        Some(i) => println!("result = {}",i),
        None => println!("noting"),
    }
    // if let 简洁控流
    
    println!("Hello, world!");
}
